<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="活动名称">
                <a-input allowClear placeholder="请输入活动名称" v-model="queryParam.name"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="活动时间">
                <a-range-picker
                  format="YYYY-MM-DD HH:mm"
                  valueFormat="YYYY-MM-DD HH:mm"
                  :placeholder="['开始时间', '结束时间']"
                  v-model="queryTime"
                  @change="changeTime"
                />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getList()">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList()">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="1"> 未开始 </a-radio-button>
          <a-radio-button value="2"> 进行中 </a-radio-button>
          <a-radio-button value="3"> 已结束 </a-radio-button>
        </a-radio-group>

        <div>
          <a-button type="primary" icon="plus" @click="edit()">新建 </a-button>
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item @click="handleAll('remove')">批量删除</a-menu-item>
            </a-menu>
            <a-button> 批量操作 </a-button>
          </a-dropdown>
        </div>
      </div>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
        size="default"
        :columns="columns"
        :data-source="list"
        :customRow="customRow"
        :scroll="{ x: 'max-content' }"
        :pagination="{
          current: pageNo,
          pageSize,
          total,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
      >
        <span slot="name" slot-scope="text">
          <a-tooltip>
            <template slot="title"> {{ text }} </template>
            {{ text }}
          </a-tooltip>
        </span>

        <span slot="time" slot-scope="text"> {{ text.start_time }}~{{ text.end_time }} </span>

        <span slot="status" slot-scope="text, record">
          <a-badge :color="getStatus(record.status)[1]" :text="getStatus(record.status)[0]" />
        </span>

        <!-- <span slot="publish" slot-scope="text, record">
          <a-switch
            @change="handleItem('change', record, record.publish === '1' ? '0' : '1')"
            :checked="record.publish === '1'"
            checked-children="开"
            un-checked-children="关"
            default-checked
          />
        </span> -->

        <span slot="addtime" slot-scope="text, record">
          {{ record.addtime > 0 ? $moment(record.addtime * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>

        <span slot="action" slot-scope="text, record">
          <a-button type="link" @click="edit(record)">编辑</a-button>
          <a-divider type="vertical" />
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:;" @click="handleItem('remove', record)">删除</a>
              </a-menu-item>
            </a-menu>
            <a class="ant-dropdown-link"> 更多 <a-icon type="down" /> </a>
          </a-dropdown>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { list, remove } from '@/api/marketing/curPrice'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '活动名称',
    dataIndex: 'name',
    scopedSlots: {
      customRender: 'name',
    },
    ellipsis: true,
  },
  {
    title: '有效时间',
    scopedSlots: {
      customRender: 'time',
    },
  },
  {
    title: '参与人数',
    dataIndex: 'userCount',
    sorter: (a, b) => parseInt(a.userCount || '0') - parseInt(b.userCount || '0'),
  },
  {
    title: '订单数量',
    dataIndex: 'orderCount',
    sorter: (a, b) => parseInt(a.orderCount || '0') - parseInt(b.orderCount || '0'),
  },
  {
    title: '订单金额',
    dataIndex: 'orderSum',
    sorter: (a, b) => parseFloat(a.orderSum || '0.00') - parseFloat(b.orderSum || '0.00'),
  },
  {
    title: '状态',
    scopedSlots: {
      customRender: 'status',
    },
  },

  // {
  //   title: '发布时间',
  //   dataIndex: 'created_at',
  //   sorter: (a, b) => a.created_at - b.created_at,
  //   width: 140,
  // },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
  },
]

export default {
  data() {
    return {
      total: 0,
      pageNo: 1,
      pageSize: 10,
      columns,
      queryParam: {
        name: '',
        status: '-1',
      },
      list: [],
      selectedRowKeys: [],
      queryTime: [],
    }
  },

  mounted() {
    this.getList()
  },

  computed: {
    getStatus() {
      return (status) => {
        let text, color
        switch (String(status)) {
          case '1':
            text = '未开始'
            color = '#ff0000'
            break

          case '2':
            text = '进行中'
            color = '#65C817'
            break

          case '3':
            text = '已结束'
            color = '#918383'
            break
        }
        return [text, color]
      }
    },
  },

  methods: {
    async edit(item) {
      let id = 0
      if (item) {
        id = item.id
      }

      this.$router.push(`/curPrice/activityEdit?id=${id}`)
    },

    changeTime(e) {
      if (e.length) {
        this.queryParam.start_time = e[0]
        this.queryParam.end_time = e[1]
      } else {
        this.queryParam.start_time = ''
        this.queryParam.end_time = ''
      }
    },

    async getList(pageNo = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize } = this
      const res = await list({
        ...queryParam,
        pageSize,
        pageNo,
      })
      if (res.code == 0) {
        this.pageNo = pageNo
        this.selectedRowKeys = []
        let { totalCount, data } = res.data
        this.total = totalCount
        this.list = data
      }
      hide()
    },

    tablePageChange(e) {
      this.getList(e)
    },

    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },

    handleAll(type) {
      let { selectedRowKeys } = this
      if (!selectedRowKeys.length) {
        this.$message.error('请至少选择一条数据~')
        return
      }

      let ids = selectedRowKeys.join(',')

      switch (type) {
        case 'remove':
          this.remove(ids)
          break
      }
    },

    handleItem(type, item) {
      switch (type) {
        case 'remove':
          this.remove(item.id)
          break
      }
    },

    remove(ids) {
      this.$confirm({
        title: '提示?',
        content: '是否删除？',
        onOk: () => {
          remove({
            ids,
          }).then((res) => {
            if (res.code) {
              this.$error({
                content: res.msg,
              })
            } else {
              this.$message.success(res.msg)
              this.getList()
              this.selectedRowKeys = []
            }
          })
        },
        onCancel: () => {},
      })
    },
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}
</style>
